<template>
  <div>
    <input type="text" v-model="msg" @keyup.enter="handleEnter" />
    <p>{{ msg }}</p>
    <div v-for="item of arr" :key="item">
      {{ item }}
      <button @click="handleDelete(item)">删除</button>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted } from "vue";
import { getLocalStorage, setLocalStorage } from "../config/storage";
export default {
  setup() {
    const state = reactive({
      msg: "",
      arr: [],
      handleEnter() {
        if (!state.arr.includes(state.msg)) {
          state.arr.push(state.msg);
          state.msg = "";
          setLocalStorage("arr", state.arr);
        }
      },
      handleDelete(item) {
        var index = state.arr.indexOf(item);
        state.arr.splice(index, 1);
        setLocalStorage("arr", state.arr);
      },
    });
    onMounted(() => {
      if (getLocalStorage("arr")) {
        state.arr = getLocalStorage("arr");
      }
    });
    const refState = toRefs(state);
    return {
      ...refState,
    };
  },
};
</script>

<style>
</style>